package org.jtb.quickr;

import java.io.IOException;
import java.io.InputStream;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

/**
 * Auth response.
 *
 * @see org.jtb.quickr.AuthService#getToken(FrobResponse)
 * @see org.jtb.quickr.AuthService#checkToken(AuthResponse.Token)
 */
public class AuthResponse extends FlickrResponse {
    /**
     * Authentication token.
     *
     * @see org.jtb.quickr.AuthService#checkToken(AuthResponse.Token)
     * @see org.jtb.quickr.UploadService#upload(AuthResponse.Token,UploadCriteria)
     * @see org.jtb.quickr.ContactsService#getList(AuthResponse.Token,ContactsGetListCriteria)
     * @see org.jtb.quickr.PhotosService#delete(AuthResponse.Token,PhotoIdResponse)
     * @see org.jtb.quickr.PhotosService#search(AuthResponse.Token,PhotosSearchCriteria)
     */
    public static class Token {
        private String tokenString;
        
        /**
         * Construct a new object from a String.
         *
         * @param tokenString The string representation of the tokenString.
         */
        public Token(String tokenString) {
            this.tokenString = tokenString;
        }
        
        /**
         * Get the string representation of this object.
         *
         * @return The tokenString as a String.
         */
        public String toString() {
            return tokenString;
        }
    }
    
    /**
     * Auth response user/
     */
    public static class User extends FlickrResponse {
        private String nsId;
        private String userName;
        private String fullName;
        
        User(Element e) throws QuickrException {
            super(e);
        }
        
        protected void populate(Element e) {
            setNsId(e.getAttributeValue("nsid"));
            setUserName(e.getAttributeValue("username"));
            setFullName(e.getAttributeValue("fullname"));
        }
        
        public String getNsId() {
            return nsId;
        }
        
        private void setNsId(String nsId) {
            this.nsId = nsId;
        }
        
        public String getUserName() {
            return userName;
        }
        
        private void setUserName(String userName) {
            this.userName = userName;
        }
        
        public String getFullName() {
            return fullName;
        }
        
        private void setFullName(String fullName) {
            this.fullName = fullName;
        }
    }
    
    private Token authToken;
    private String perms;
    private User user;
    
    AuthResponse(InputStream is) throws QuickrException{
        super(is);
    }
    
    protected void populate(Element e) throws QuickrException {
        Element authElement = e.getChild("auth");
        
        if (authElement != null) {
            Element tokenElement = authElement.getChild("token");
            if (tokenElement != null) {
                setAuthToken(new AuthResponse.Token(tokenElement.getText()));
            }
            
            Element permsElement = authElement.getChild("perms");
            if (permsElement != null) {
                setPerms(permsElement.getText());
            }
            
            Element userElement = authElement.getChild("user");
            if (userElement != null) {
                setAuthUser(new User(userElement));
            }
        }
    }
    
    public AuthResponse.Token getAuthToken() {
        return authToken;
    }
    
    public void setAuthToken(AuthResponse.Token authToken) {
        this.authToken = authToken;
    }
    
    public String getPerms() {
        return perms;
    }
    
    public void setPerms(String perms) {
        this.perms = perms;
    }
    
    public User getAuthUser() {
        return user;
    }
    
    public void setAuthUser(User authUser) {
        this.user = authUser;
    }
}
